package com.itheima.homework;

import java.util.Arrays;
import java.util.Random;

public class Test2 {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();

        new Thread(myRunnable, "前门").start();
        new Thread(myRunnable, "后门").start();
    }
}

// 任务类
class MyRunnable implements Runnable {
    int count1 = 0;// 记录的是前门入场人数
    int count2 = 0;// 记录的是后门入场人数
    int number = 0;// 总人数  99

    @Override
    public void run() {
        while (true) {
            synchronized (MyRunnable.class) {
                String name = Thread.currentThread().getName();
                if (number == 100) {
                    if (name.equals("前门")) {
                        System.out.println("从前门入场的员工总共:" + count1 + "位员工 ");
                        System.out.println("从后门入场的员工总共:" + count2 + "位员工 ");
                    }
                    break;
                } else {
                    number++;// 100
                    System.out.println("编号为: " + number + "的员工从" + name + "入场! 拿到的双色球彩票号码是:" + DoubleColorBallUtil.create());
                }
                if (name.equals("前门")) {
                    count1++;
                } else {
                    count2++;
                }
            }
        }
    }
}


// 产生双色球的类
class DoubleColorBallUtil {
    // 产生双色球的代码
    public static String create() {
        String[] red = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33"};
        //创建蓝球
        String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
        boolean[] used = new boolean[red.length];
        Random r = new Random();
        String[] all = new String[7];
        for (int i = 0; i < 6; i++) {
            int idx;
            do {
                idx = r.nextInt(red.length);//0‐32
            } while (used[idx]);//如果使用了继续找下一个
            used[idx] = true;//标记使用了
            all[i] = red[idx];//取出一个未使用的红球
        }
        all[all.length - 1] = blue[r.nextInt(blue.length)];
        Arrays.sort(all);
        return Arrays.toString(all);
    }
}
